package com.yicong.ants.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.databinding.DataBindingUtil;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.cchao.simplelib.core.UiHelper;
import com.yicong.ants.R;
import com.yicong.ants.databinding.CardFuncItemBinding;

/**
 * 通用控件
 *
 * <attr name="left_label" format="string"/>
 * <attr name="input_hint" format="string"/>
 */
public class CardFuncItemView extends LinearLayout {

    public CardFuncItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public CardFuncItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public CardFuncItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    /**
     * 初始化控件
     */
    private void initView(Context context, AttributeSet attrs) {
        setGravity(Gravity.CENTER);
        CardFuncItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.card_func_item, this, true);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CardFuncItemView);
        if (typedArray != null && typedArray.length() > 0) {
            for (int i = 0; i < typedArray.getIndexCount(); i++) {
                int id = typedArray.getIndex(i);
                switch (id) {
                    case R.styleable.CardFuncItemView_text1:
                        binding.text1.setText(typedArray.getString(id));
                        break;
                    case R.styleable.CardFuncItemView_text2:
                        binding.text2.setText(typedArray.getString(id));
                        break;
                    case R.styleable.CardFuncItemView_image:
                        binding.image.setImageResource(typedArray.getResourceId(id, R.drawable.func_free));
                        break;
                    case R.styleable.CardFuncItemView_text1_color:
                        binding.text1.setTextColor(typedArray.getColor(id, UiHelper.getColor(R.color.text_2a)));
                        break;
                    default:
                        break;
                }
            }
            typedArray.recycle();
        }
    }

}
